<template>
  <!-- 表单 -->
  <el-form ref="refForm" label-width="120px" :model="formData">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间">
      <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式">
      <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
        <!-- <el-option label="正式" value="1" />
        <el-option label="非正式" value="2" />
        <el-option label="兼职" value="3" /> -->
        <el-option
          v-for="item in hireTypes"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号">
      <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门">
      <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="hFocus" />
      <div v-show="isShowTree">
        <el-tree :props="{label: 'name'}" :data="departments" @node-click="handleNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button>取消</el-button>
      <el-button type="primary" @click="hAdd">确定</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import employeeENUM from '@/constant/employees'
import { addEmployee } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      departments: [],
      hireTypes: employeeENUM.hireType,
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      isShowTree: false
    }
  },
  created() {
    // this.loadDepartments()
  },
  methods: {
    resetForm() {
      this.$refs.refForm.resetFields()
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
    },
    handleNodeClick(node) {
      console.log(node)
      // if (节点的children为空) {
      //   // 把当前节点数据填入表单
      // }
      if (!node.children.length) {
        this.formData.departmentName = node.name

        // 隐藏tree
        this.isShowTree = false
      }
    },
    hFocus() {
      this.loadDepartments()
    },
    async loadDepartments() {
      this.isShowTree = false

      // 调用接口，给list赋值
      const res = await getDepartments()
      // 第一项是公司的名字，不应该是某个部门，所以去掉它。
      res.data.depts.shift()

      this.departments = tranListToTreeData(res.data.depts)
      this.isShowTree = true
    },
    hAdd() {
      // 0. 表单校验
      this.doAdd()
    },
    async doAdd() {
      await addEmployee(this.formData)
      this.$emit('success')
    }

  }
}
</script>
